package persistencia;

import java.sql.*;
import java.util.*;

public class Conexao {

    private static Properties param = new Properties();
    private static Connection conn = null;

    private static void conecta() {
        try{
            param.load(Conexao.class.getResourceAsStream("banco.properties"));
            String driver = param.getProperty("driver");
            String url = param.getProperty("url");
            String login = param.getProperty("login");
            String senha = param.getProperty("senha");
            Class.forName(driver);
            conn = DriverManager.getConnection(url, login, senha);
            System.out.println("Conectou");
        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();

        } catch (Exception e) {
            System.out.println("Problemas com o BD:");
            e.printStackTrace();
        
        }     
    }

    public static void desconecta() {
        try {
            if (conn != null) {
                conn.close();
                conn = null;
            }
            System.out.println("Desconectou");
        } catch (SQLException ex) {
            System.err.println(ex);
        }

    }

    public static Connection getConexao() {
        if (conn==null){
            conecta();
        } else{
            try{
                if(conn.isClosed()){
                conecta();
        }
            }catch(Exception e){
                e.printStackTrace();
            }
            
        }
        return conn;
    }
    
    public static void main(String[] args) {
        try {
            
        Connection conn = getConexao();
        if(conn==null){
        System.out.println("nao esta conectado" );
        }else{
        System.out.print("conexao com sucesso");}
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }
    
}
